<?php

namespace App\Http\Middleware;
use Session;
use Illuminate\Support\Facades\Request;
use Closure;
use App\Http\Service\Check\CheckService;

class LoginMiddleware
{
    private  $check;
    public function __construct(CheckService $checkService)
    {
        $this->check = $checkService;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if(Session::has('member_token') && !empty(Session::get('member_token')))
        {
            return $next($request);
//            $where = [];
//            $where['token'] = Session::get('member_token');
//            $data = $this->check->CheckAdminStatus($where);
//
//            if($data['data'] == 'SUCCESS')
//            {
//                return $next($request);
//            }else{
//
//                if(Request::ajax())
//                {
//                    $arr = ['error_code'=>'10001','error_message'=>'账户已过期,请从新登陆'];
//                    return response()->json($arr);
//                }else{
//                    echo "<script>confirm('账户已过期,请从新登陆')?window.parent.location.href='/' : history.back() ;</script>";
//                    exit();
//                }
//
//            }
        }else{
            $arr = ['error_code'=>'10001','error_message'=>'账户已过期,请重新登陆'];
            if(Request::ajax())
            {
                return response()->json($arr);
            }else{
                echo "<script>confirm('{$arr['error_message']}')?window.parent.location.href='/' : history.back() ;</script>";
                exit();
            }



        }
    }
}
